package com.xinghai.web.controller.dingtalk;

import com.xinghai.common.annotation.Log;
import com.xinghai.common.core.controller.BaseController;
import com.xinghai.common.core.domain.AjaxResult;
import com.xinghai.common.core.page.TableDataInfo;
import com.xinghai.common.enums.BusinessType;
import com.xinghai.common.exception.ServiceException;
import com.xinghai.common.utils.poi.ExcelUtil;
import com.xinghai.system.domain.XhContractInfo;
import com.xinghai.system.service.IXhContractInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 合同信息管理Controller
 * 
 * @author xinghai
 * @date 2024-08-01
 */
@RestController
@RequestMapping("/system/contractInfo")
public class XhContractInfoController extends BaseController
{
    @Autowired
    private IXhContractInfoService xhContractInfoService;

    /**
     * 查询合同信息管理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(XhContractInfo xhContractInfo)
    {
        startPage();
        List<XhContractInfo> list = xhContractInfoService.list(xhContractInfo);
        return getDataTable(list);
    }

    /**
     * 导出合同信息管理列表
     */
    @Log(title = "合同信息管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, XhContractInfo xhContractInfo)
    {
        List<XhContractInfo> list = xhContractInfoService.list(xhContractInfo);
        ExcelUtil<XhContractInfo> util = new ExcelUtil<XhContractInfo>(XhContractInfo.class);
        util.exportExcel(response, list, "合同管理数据");
    }

    /**
     * 获取合同信息管理详细信息
     */
    @GetMapping("/getInfo")
    public AjaxResult getInfo(Long id)
    {
        return success(xhContractInfoService.getInfo(id));
    }

    /**
     * 新增合同信息管理
     */
    @Log(title = "合同信息管理", businessType = BusinessType.INSERT)
    @PostMapping("/insert")
    public AjaxResult add(@RequestBody XhContractInfo xhContractInfo)
    {
        return toAjax(xhContractInfoService.insert(xhContractInfo));
    }

    /**
     * 修改合同信息管理
     */
    @Log(title = "合同信息管理", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public AjaxResult update(@RequestBody XhContractInfo xhContractInfo)
    {
        return toAjax(xhContractInfoService.update(xhContractInfo));
    }

    /**
     * 删除合同信息管理
     */
    @Log(title = "合同信息管理", businessType = BusinessType.DELETE)
	@PostMapping("/delete")
    public AjaxResult delete(Long id)
    {
        return toAjax(xhContractInfoService.delete(id));
    }


    /**
     * 批量删除合同信息管理
     */
    @Log(title = "合同信息管理", businessType = BusinessType.DELETE)
    @PostMapping("/deleteBatch")
    public AjaxResult deleteBatch(Long[] ids)
    {
        return toAjax(xhContractInfoService.deleteBatch(ids));
    }

    /**
     * 导入合同信息管理模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<XhContractInfo> util = new ExcelUtil<XhContractInfo>(XhContractInfo.class);
        util.importTemplateExcel(response, "合同信息管理数据模板");
    }


    /**
     * 导入合同信息管理
     */
    @Log(title = "合同信息管理管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file)
    {
        ExcelUtil<XhContractInfo> util = new ExcelUtil<XhContractInfo>(XhContractInfo.class);
        List<XhContractInfo> xhContractInfoList = null;
        try {
            xhContractInfoList = util.importExcel(file.getInputStream());
        } catch (IOException e) {
            throw new ServiceException("导入数据失败");
        }
        return success(xhContractInfoService.importData(xhContractInfoList));
    }
}
